package com.naodong.shenluntiku.mvp.presenter;

import com.naodong.shenluntiku.mvp.contract.StudySetContract;
import com.naodong.shenluntiku.mvp.model.bean.StudySet;
import com.naodong.shenluntiku.mvp.model.error.ApiError;
import com.naodong.shenluntiku.mvp.model.error.DefaultResponseErrorListener;
import com.naodong.shenluntiku.mvp.model.error.ResponseErrorListener;
import com.naodong.shenluntiku.mvp.model.error.ResultHandleSubscriber;
import com.naodong.shenluntiku.util.RxUtils;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import me.shingohu.man.di.scope.ActivityScope;
import me.shingohu.man.intergation.retrofit.NoBodyEntity;
import me.shingohu.man.mvp.BasePresenter;
import me.shingohu.man.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/30.
 */

@ActivityScope
public class StudySetPresenter extends BasePresenter<StudySetContract.Model, StudySetContract.View> {
    @Inject
    public StudySetPresenter(StudySetContract.Model model, StudySetContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 获取学习设置
     */
    public void requestStudySet() {
        addDisposable(RxUtils.applySchedulers(mModel.getStudySet(), getView()).subscribeWith(new ResultHandleSubscriber<StudySet>(ResponseErrorListener.EMPTY) {
            @Override
            public void onNext(@NonNull StudySet studySet) {
                getView().onStudySet(studySet);
            }
        }));
    }

    /**
     * 更新学习设置
     * @param value
     */
    public void requestUpdateStudySet(String type, int isChosen, String value) {
        RxUtils.applyNoBodySchedulers(mModel.updateStudySet(type, isChosen, value), getView()).subscribeWith(new ResultHandleSubscriber<NoBodyEntity>(new DefaultResponseErrorListener() {
            @Override
            public void handleResponseError(ApiError apiError) {
                ToastUtil.makeToast("设置失败");
                getView().onUpdateStudyFail();
            }
        }
        ) {
            @Override
            public void onNext(@NonNull NoBodyEntity noBodyEntity) {
                ToastUtil.makeToast("设置成功");
                getView().onUpdateStudySet(type, isChosen, value);
            }
        });
    }
}
